package com.chexiehui.jc.ui.fragment;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.blankj.utilcode.util.SizeUtils;
import com.chexiehui.jc.R;
import com.chexiehui.jc.adapter.CommVLayoutAdapter;
import com.chexiehui.jc.base.BaseModel;
import com.chexiehui.jc.base.BaseMvpFragment;
import com.chexiehui.jc.entity.BrandInfo;
import com.chexiehui.jc.entity.CarInfo;
import com.chexiehui.jc.entity.CarSeriesInfo;
import com.chexiehui.jc.mvp.mine.ChooseCarModelCarCovenant;
import com.chexiehui.jc.mvp.mine.ChooseCarModelCarPresenter;
import com.chexiehui.jc.utils.GlideUtil;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 选择车
 */
public class ChooseCarModelCarFragment extends BaseMvpFragment<ChooseCarModelCarPresenter> implements ChooseCarModelCarCovenant.View {


    @BindView(R.id.tv_series_name)
    TextView tvSeriesName;
    @BindView(R.id.tv_series_price)
    TextView tvSeriesPrice;
    @BindView(R.id.recycler)
    RecyclerView mRecycler;
    private DelegateAdapter listAdapter;
    private CommVLayoutAdapter<CarInfo> carListAdapter;
//    private BrandInfo brandInfo;
    private CarSeriesInfo carSeriesInfo;
    private OnCarChooseListener onCarChooseListener;

    public static ChooseCarModelCarFragment getInstance(CarSeriesInfo carSeriesInfo) {
        ChooseCarModelCarFragment fragment = new ChooseCarModelCarFragment();
        Bundle bundle = new Bundle();
//        bundle.putSerializable("brand", brandInfo);
        bundle.putSerializable("entity", carSeriesInfo);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void beforeSetView() {
        Bundle bundle = getArguments();
        if (bundle != null) {
//            brandInfo = (BrandInfo) bundle.get("brand");
            carSeriesInfo = (CarSeriesInfo) bundle.get("entity");
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_choose_car_model_car;
    }

    @Override
    protected void initView(Bundle var1) {
        if (carSeriesInfo == null) {
            return;
        }
        tvSeriesName.setText(carSeriesInfo.getName());
        tvSeriesPrice.setText(carSeriesInfo.getPriceMaxMinStr());
        VirtualLayoutManager virtualLayoutManager = new VirtualLayoutManager(mActivity);
        mRecycler.setLayoutManager(virtualLayoutManager);
        ((SimpleItemAnimator) mRecycler.getItemAnimator()).setSupportsChangeAnimations(false);
        final RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
        mRecycler.setRecycledViewPool(viewPool);
        viewPool.setMaxRecycledViews(0, 20);
        listAdapter = new DelegateAdapter(virtualLayoutManager, false);
        mRecycler.setAdapter(listAdapter);
        int dp_10 = SizeUtils.dp2px(10);
        LinearLayoutHelper helper = new LinearLayoutHelper(SizeUtils.dp2px(1));
        helper.setMargin(0, dp_10, 0, dp_10);
        helper.setBgColor(ContextCompat.getColor(mActivity, R.color.colorTransparent));
        carListAdapter = new CommVLayoutAdapter<CarInfo>(mActivity, new ArrayList<>(),
                R.layout.item_choose_car_model_car, helper) {
            @Override
            public void convert(ViewHolder holder, int position, CarInfo item) {
               /* GlideUtil.loadImageViewCrop(mActivity, item.getImg(), holder.getView(R.id.iv_item_image),
                        R.drawable.shape_square, R.drawable.shape_square);*/
                holder.setText(R.id.tv_item_name, item.getName());
                holder.setText(R.id.tv_item_price, item.getNewPriceStrUnit());
            }
        };
        carListAdapter.setItemClickListener((view, position, item) -> {
            if (onCarChooseListener != null) {
                onCarChooseListener.onCarChoose(item);
            }
        });
        listAdapter.addAdapter(carListAdapter);
        if (carSeriesInfo == null) {
            return;
        }
        mvpPresenter.getList();
    }

    @Override
    protected ChooseCarModelCarPresenter createPresenter() {
        return new ChooseCarModelCarPresenter(this);
    }

    public void setOnChooseListener(OnCarChooseListener listener) {
        this.onCarChooseListener = listener;
    }

    @Override
    public void onGetListSuccess(BaseModel<List<CarInfo>> successResult) {
        carListAdapter.getData().clear();
        carListAdapter.getData().addAll(successResult.getData());
        carListAdapter.notifyDataSetChanged();
    }

    @Override
    public void onGetListFailure(BaseModel<Object> failureResult) {

    }

    @Override
    public String getSeriesId() {
        return carSeriesInfo.getId();
    }


    public interface OnCarChooseListener {
        void onCarChoose(CarInfo carInfo);
    }

}
